<!-- 政策详情  -->
<template>
  <view class="container screen-width">
    <view class="main_content">
      <!-- 政策通知 -->
      <view v-if="details">
        <view class="main_head">
          <view class="title">{{detailsData.title}}</view>
          <view class="subTitle">
            受理部门: {{detailsData.departmentName}}
            <span>
              <p
                @tap="isDialogue(detailsData,detailsData.whetherAttention,detailsData.departmentId)"
              >
                <image
                  src="../../static/protyDetails/add.png"
                  v-if="detailsData.whetherAttention==0"
                />
                {{detailsData.whetherAttention==0?'订阅':'已订阅'}}
              </p>
            </span>
          </view>
          <view class="time_text">
            申报时间: {{detailsData.releaseTime}}
            <span @tap="subscribeMore">订阅更多</span>
            <span
              class="export"
              v-if="detailsData.whetherCanExport === 1"
              @tap="toExport(detailsData.policyId)"
            >{{downName===1?'符合申报条件企业名单':'符合下一批申报企业名单'}}</span>
          </view>
        </view>
        <view class="main_body">
          <!-- 通知 -->
          <view class="main_item" v-html="hcontent"></view>
          <view class="readNum">
            <view class="readNum_L">
              <span>阅读量:{{detailsData.pageViewCount}}</span>
            </view>
            <view class="readNum_R">
              <a :href="detailsData.link" v-if="detailsData&&detailsData.link">阅读原文</a>
            </view>
          </view>
        </view>
      </view>
      <!-- 政策解读 -->
      <view v-if="!details">
        <view class="main_head">
          <view class="title">{{getUnscrambleInfo.title}}</view>
          <view class="subTitle" @tap="isDialogue">
            受理部门: {{getUnscrambleInfo.departmentName}}
            <span>
              <p
                @tap="isDialogue(getUnscrambleInfo,getUnscrambleInfo.whetherAttention,getUnscrambleInfo.departmentId)"
              >
                <image
                  src="../../static/protyDetails/add.png"
                  v-if="getUnscrambleInfo.whetherAttention==0"
                />
                {{getUnscrambleInfo.whetherAttention==0?'订阅':'已订阅'}}
              </p>
            </span>
          </view>
          <view class="time_text">
            申报时间: {{getUnscrambleInfo.releaseTime || "--"}}
            <span @tap="subscribeMore">订阅更多</span>
            <span
              class="export"
              v-if="getUnscrambleInfo.whetherCanExport === 1"
              @tap="toExport(getUnscrambleInfo.policyId)"
            >该项目可申报企业名单</span>
          </view>
        </view>
        <view class="main_body">
          <view v-if="getUnscrambleInfo.length != 0">
            <view class="main_item">
              <view class="items">申报条件</view>
              <view class="content">{{getUnscrambleInfo.declareCondition}}</view>
            </view>
            <view class="main_item">
              <view class="items">支持力度</view>
              <view class="content">{{getUnscrambleInfo.supported}}</view>
            </view>
            <view class="main_item">
              <view class="items">申报材料</view>
              <view class="content">{{getUnscrambleInfo.materials}}</view>
            </view>
            <view class="main_item">
              <view class="items">项目来源</view>
              <view class="content">{{getUnscrambleInfo.sourceFrom}}</view>
            </view>
          </view>
          <view v-if="detailsInfo.length != 0"></view>
          <view class="readNum">
            <text>阅读量：{{getUnscrambleInfo.pageViewCount}}</text>
          </view>
        </view>
      </view>
    </view>
    <!-- 订阅部门选取关键字 -->
    <keyWord ref="keyword" @onSubmit="attention"></keyWord>
    <Festival></Festival>
  </view>
</template>
<script>
import topBar from "../../components/topBar.vue";
import dialogue from "../../components/dialog.vue";
import { userInfo } from "../../util/api/user.js";
import wx from "jweixin-npm";
import { isWeixin, wxShareConfig } from "@/util/wx.js";
import { fileUrl } from "@/util/api.js";
import {
  getPolicyInterpretInfo,
  queryPolicyNoticeDetail,
  whetherDownload,
  whetherCanExport,
  getPolicyCompanyUrl,
  departmentAttention,
  cancelAttention
} from "../../util/api/policy.js";
export default {
  components: {
    topBar,
    dialogue
  },
  data() {
    return {
      options: {}, // 声明一个对象id
      detailsInfo: {}, // 通知信息
      getUnscrambleInfo: {}, // 解读信息
      detailsData: {},
      details: true, // 是否为通知
      hcontent: "",
      id: "",
      inform: "",
      downName: 1, // 下载名单 2公示：下一批符合条件可申报企业名单 1通知：符合申报条件企业名单
      title: "" //分享标题
    };
  },

  onLoad(option) {
    this.id = option.id;
    this.inform = option.inform;

    if (this.inform == "通知") {
      // 政策通知
      this.details = true;
      this.getdetails(this.id);
    } else {
      // 解读
      this.getUnscramble(this.id);
      this.details = false;
    }
  },
  //方法集合
  methods: {
    // 点击订阅更多
    subscribeMore() {
      uni.navigateTo({
        url: "/pages/policy/policySubscription"
      });
    },
    /* 通知 */
    getdetails(val) {
      queryPolicyNoticeDetail({ policyId: val }).then(res => {
        if (res.code == 200) {
          this.detailsData = res.result;
          // 判断标题关键字
          if (this.detailsData.title.includes("公示")) {
            this.downName = 2;
          } else {
            this.downName = 1;
          }
          this.hcontent = this.decodeUnicode(res.result.content);
          if (this.detailsData.title) {
            this.title = this.detailsData.title;
            if (isWeixin()) {
              this.initShareConfig();
            }
          }
        }
      });
    },
    decodeUnicode(str) {
	  if(!str) return null;
      var result = "";
      var code = str.match(/&#(\d+);/g);
      if (code == null) {
        return;
      }
      for (var i = 0; i < code.length; i++) {
        result = result + String.fromCharCode(code[i].replace(/[&#;]/g, ""));
      }
      return result;
    },
    /* 解读详情 */
    getUnscramble(val) {
      getPolicyInterpretInfo({ id: val }).then(res => {
        if (res.code == 200) {
          this.getUnscrambleInfo = res.result;
          this.getUnscrambleInfo.declareCondition.replace(/\r/g, "<br>");
          this.getUnscrambleInfo.supported.replace(/\r/g, "<br>");
          this.getUnscrambleInfo.materials.replace(/\r/g, "<br>");
          this.getUnscrambleInfo.sourceFrom.replace(/\r/g, "<br>");
          if (this.getUnscrambleInfo.title) {
            this.title = this.getUnscrambleInfo.title;
            if (isWeixin()) {
              this.initShareConfig();
            }
          }
        }
      });
    },
    // 点击订阅
    isDialogue(list, val, id) {
      // 判断用户是否有登录
      let token = this.$getToken();
      if (!token) {
        this.isLogin();
        return;
      }
      if (val == 1) {
        uni.showModal({
          title: "提示",
          content: "您确定要取消订阅吗,取消后将不再接收该部门的政策推送",
          success(res) {
            if (res.confirm) {
              cancelAttention(id).then(res => {
                if (res.code == 200) {
                  list.whetherAttention = 0;
                }
              });
            }
          }
        });
      } else if (val == 0) {
        this.$refs.keyword.open(id);
      }
    },
    attention(id, keyWord) {
      let params = {
        departmentId: id,
        keywords: keyWord
      };
      departmentAttention(params).then(res => {
        if (res.code == 200) {
          // list.whetherAttention = 1;
          if (this.inform == "通知") {
            // 政策通知
            this.details = true;
            this.getdetails(this.id);
          } else {
            // 解读
            this.getUnscramble(this.id);
            this.details = false;
          }
          uni.showToast({ title: "操作成功", icon: "none" });
        } else {
          uni.showModal({
            title: "提示",
            content: res.message,
            confirmText: "升级会员",
            success: function(res) {
              if (res.confirm) {
                uni.navigateTo({
                  url: "/pages/VIP/index"
                });
              }
            }
          });
        }
      });
    },
    // 当没有登录的情况下调用
    isLogin() {
      uni.setStorageSync("pageUrl", window.location.href);
      // 去登录页
      uni.navigateTo({
        url: "/pages/login/index"
      });
    },
    initShareConfig() {
      let _this = this;
      let windowurl = window.location.href.split("#")[0];
      uni.request({
        url: "https://oldm.51bmj.cn/WeiXin/GetClientSign", //仅为示例，并非真实接口地址。
        data: {
          rawUrl: windowurl
        },
        dataType: "json",
        header: {
          "X-Requested-With": "XMLHttpRequest"
        },
        method: "GET",
        success: res => {
          console.log(888888888, res);
          if (res.statusCode == 200) {
            let { data } = res;
            let shareInfo = {
              title: _this.title,
              desc: "政策尽知 红利尽享",
              link: window.location.href
            };
            console.log(99999999, data, shareInfo);
            wxShareConfig(this.$wx, data, shareInfo);
          }
        }
      });
    },
    toExport(policyId) {
      whetherCanExport().then(res => {
        if (res.code === 10001) {
          uni.showToast({
            title: "您还没有登录，请登录后操作",
            duration: 2000,
            icon: "none"
          });
          setTimeout(() => {
            uni.setStorageSync("pageUrl", window.location.href);
            uni.navigateTo({
              url: "/pages/login/index"
            });
          }, 2000);
        } else if (res.code === 10006) {
          uni.showModal({
            title: "提示",
            content: "可下载数量已满",
            confirmText: "升级会员",
            success: function(res) {
              if (res.confirm) {
                // 升级会员
                uni.navigateTo({
                  url: "/pages/VIP/index"
                });
              }
            }
          });
        } else if (res.code === 200) {
          whetherDownload({ source: "1", policyId: policyId });
          getPolicyCompanyUrl({ policyId }).then(res => {
            if (res.code == 200 && res.result) {
              window.location.href = fileUrl + res.result;
            } else {
              uni.showToast({
                title: "企业名单未生成，请稍后重试！",
                duration: 2000,
                icon: "none"
              });
            }
          });
        }
      });
    }
  }
};
</script>
<style>
body {
  user-select: text;
}
</style>
<style lang="less" scoped>
//@import url(); 引入公共css类
.main_content {
  background-color: #fff;
  user-select: text;

  .main_head {
    padding: 0 30rpx 50rpx;
    background-color: #fff;
    border-bottom: 1rpx solid #ccc;
    margin-bottom: 54rpx;
    user-select: text;

    .title {
      padding: 40rpx 0;
      font-size: 20px;
      color: #333;
      font-weight: 700;
    }

    .subTitle {
      max-width: 690rpx;
      height: 80rpx;
      display: flex;
      align-items: center;
      background-color: #eef0fc;
      justify-content: space-around;
      font-size: 14px;
      color: #333;
      border-radius: 10rpx;

      span {
        display: flex;
        align-items: center;
        justify-content: space-around;
        font-size: 14px;
        width: 120rpx;
        height: 48rpx;
        background-color: #1678ff;
        border-radius: 10rpx;
        color: #fff;

        image {
          width: 8px;
          height: 8px;
          // font-size: 0;

          // margin-top:30rpx;
        }
      }
    }

    .time_text {
      margin-top: 30rpx;
      color: #aaaaaa;
      height: 60rpx;
      font-size: 13px;

      span {
        color: #227fff;
        margin-left: 20rpx;
      }
      .export {
        color: red;
        font-size: 32rpx;
      }
    }
  }

  .main_body {
    padding: 0 30rpx;
    user-select: text;
    box-sizing: border-box;
    overflow: scroll;
    .main_item {
      min-height: 626rpx;
      user-select: text;
      font-size: 16px;

      .items {
        padding-left: 20rpx;
        height: 80rpx;
        display: flex;
        align-items: center;
        font-size: 15px;
        font-weight: 700;
        background-color: #e9f3ff;
        color: #227fff;
      }

      .content {
        font-size: 14px;
        margin: 20rpx auto;
        word-wrap: break-word;
        white-space: pre-wrap;
      }
    }

    .readNum {
      padding: 60rpx 0;
      color: #ccc;
      font-size: 13px;
      display: flex;
      // justify-content: space-between;
      border-bottom: 1rpx solid #f0f0f0;
      margin-bottom: 30rpx;

      .readNum_L {
        display: flex;
        justify-content: flex-start;
        align-items: center;

        text {
          display: flex;
          align-items: center;
          margin-right: 30rpx;
          color: #ccc;
        }
      }

      .readNum_R {
        margin-left: 10rpx;

        a {
          font-size: 14px;
          color: #1678ff;
        }
      }

      view.shareBtn {
        margin-left: 30rpx;
        display: flex;
        align-items: center;
        position: relative;
        height: 30rpx;

        button {
          position: absolute;
          left: 25rpx;
          opacity: 0;
          top: 0;
        }
      }

      image {
        width: 36rpx;
        height: 36rpx;
        margin-left: 20rpx;
        // margin-top:10rpx;
      }
    }
  }
}
.wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}
</style>
